/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef NWEB_EXTENSION_DOWNLOADS_CEF_DELEGATE_H
#define NWEB_EXTENSION_DOWNLOADS_CEF_DELEGATE_H
#pragma once

#include "base/functional/callback.h"
#include "ohos_nweb/src/capi/browser_service/nweb_extension_downloads_types.h"
#if BUILDFLAG(ARKWEB_NWEB_EX)
#include "ohos_nweb_ex/public/capi/nweb_extension_downloads_callbacks.h"
#endif

typedef base::OnceCallback<void(const FilenameSuggestion& suggestion)>
      FilenameChangedCallback;

using DownloadEraseCallback = base::RepeatingCallback<
    void(std::optional<std::string> error, std::vector<int32_t>& eraseIds)>;

using DownloadsOpenCallback = base::RepeatingCallback<void(std::optional<std::string> error)>;
using DownloadsRemoveFileCallback =
    base::RepeatingCallback<void(std::optional<std::string> error)>;
using DownloadsPauseCallback = base::RepeatingCallback<void(std::optional<std::string> error)>;
using DownloadsResumeCallback =
    base::RepeatingCallback<void(std::optional<std::string> error)>;
using DownloadsCancelCallback =
    base::RepeatingCallback<void(std::optional<std::string> error)>;
using DownloadsAcceptDangerCallback =
    base::RepeatingCallback<void(std::optional<std::string> error)>;
using DownloadsSetUiOptionsCallback =
    base::RepeatingCallback<void(std::optional<std::string> error)>;
using DownloadsShowCallback = base::RepeatingCallback<void(std::optional<std::string> error)>;
using DownloadSearchCallback =
    base::RepeatingCallback<void(std::optional<std::string> error,
                                 const uint32_t size,
                                 std::vector<ExDownloadsItem>& downloadItems)>;
using DownloadGetFileIconCallback =
    base::RepeatingCallback<void(std::optional<std::string> error,
                                 std::string iconUrl)>;

using DownloadsIdCallback =
    base::RepeatingCallback<void(std::optional<std::string> error, int downloadId)>;

namespace OHOS::NWeb {
class NWebExtensionDownloadCefDelegate {
 public:
  static NWebExtensionDownloadCefDelegate& GetInstance();

  // downloads.erase
  bool Erase(ExDownloadsQueryInfo& query, DownloadEraseCallback callback);
  void EraseCallback(int requestId,
                     std::optional<std::string> error,
                     std::vector<int32_t>& eraseIds);

  // downloads.open
  bool Open(int downloadId,
            const std::optional<std::string>& contextType,
            const std::optional<bool>& includeIncognitoInfo,
            DownloadsOpenCallback callback);
  void OpenCallback(int requestId, std::optional<std::string> error);

  // downloads.removeFile
  bool RemoveFile(int downloadId,
                  const std::optional<std::string>& contextType,
                  const std::optional<bool>& includeIncognitoInfo,
                  DownloadsRemoveFileCallback callback);
  void RemoveFileCallback(int requestId, std::optional<std::string> error);

  // downloads.pause
  bool Pause(int downloadId,
             const std::optional<std::string>& contextType,
             const std::optional<bool>& includeIncognitoInfo,
             DownloadsPauseCallback callback);
  void PauseCallback(int requestId, std::optional<std::string> error);

  // downloads.resume
  bool Resume(int downloadId,
              const std::optional<std::string>& contextType,
              const std::optional<bool>& includeIncognitoInfo,
              DownloadsResumeCallback callback);
  void ResumeCallback(int requestId, std::optional<std::string> error);

  // downloads.cancel
  bool Cancel(int downloadId,
              const std::optional<std::string>& contextType,
              const std::optional<bool>& includeIncognitoInfo,
              DownloadsCancelCallback callback);
  void CancelCallback(int requestId, std::optional<std::string> error);

  // downloads.acceptDanger
  bool AcceptDanger(int downloadId,
                    const std::optional<std::string>& contextType,
                    const std::optional<bool>& includeIncognitoInfo,
                    DownloadsAcceptDangerCallback callback);
  void AcceptDangerCallback(int requestId, std::optional<std::string> error);

  // downloads.setUiOptions
  bool SetUiOptions(const ExDownloadsUiOptions& options,
                    DownloadsSetUiOptionsCallback callback);
  void SetUiOptionsCallback(int requestId, std::optional<std::string> error);

  // downloads.show
  bool Show(int downloadId,
            const std::optional<std::string>& contextType,
            const std::optional<bool>& includeIncognitoInfo,
            DownloadsShowCallback callback);
  void ShowCallback(int requestId, std::optional<std::string> error);

  // downloads.showDefaultFolder
  void ShowDefaultFolder();

  // downloads.search
  bool Search(ExDownloadsQueryInfo& query, DownloadSearchCallback callback);
  void SearchCallback(int requestId,
                      std::optional<std::string> error,
                      const uint32_t size,
                      std::vector<ExDownloadsItem>& downloadItems);

  // downloads.download
  bool GetDownloadId(const std::string& guid, DownloadsIdCallback callback);
  void GetDownloadIdCallback(int requestId, std::optional<std::string> error, int downloadId);

  // downloads.getFileIcon
  bool GetFileIcon(ExDownloadsGetFileIconOptions& iconOption,
                   DownloadGetFileIconCallback callback);
  void GetFileIconCallback(int requestId,
                           std::optional<std::string> error,
                           std::string iconUrl);

  // getAllDownloadItem
  ExDownloadsItemVector GetAllDownloadItem();

  // downloads.OnCreated
  void OnCreated(ExDownloadsItem* item);

  // downloads.OnChanged
  void OnChanged(ExDownloadsItem* item);

  // downloads.OnErased
  void OnErased(int downloadId);

  // downloads.OnDeterminingFilename
  void OnDeterminingFilename(const ExDownloadsItem* item,
                             const char* suggestedPath,
                             FilenameChangedCallback callback);
};

}  // namespace OHOS::NWeb

#endif  // NWEB_EXTENSION_DOWNLOADS_CEF_DELEGATE_H